﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Data;
using System.Xml;

namespace Comm
{
    /// <summary>
    /// QQMSN实体类
    /// </summary>
    public class MQQMSN
    {
        private string _gid;
        private string _number;
        private string _alt;
        private int _ordinal;
        private bool _enabled;
        private int _type;

        /// <summary>
        /// 主键GUID
        /// </summary>
        public string GID
        {
            set { _gid = value; }
            get { return _gid; }
        }

        /// <summary>
        /// QQ，MSN账号
        /// </summary>
        public string Number
        {
            set { _number = value; }
            get { return _number; }
        }

        /// <summary>
        /// Alt
        /// </summary>
        public string Alt
        {
            set { _alt = value; }
            get { return _alt; }
        }

        /// <summary>
        /// 排序
        /// </summary>
        public int Ordinal
        {
            set { _ordinal = value; }
            get { return _ordinal; }
        }

        /// <summary>
        /// 启用
        /// </summary>
        public bool Enabled
        {
            set { _enabled = value; }
            get { return _enabled; }
        }

        /// <summary>
        /// 类型
        /// </summary>
        public int Type
        {
            set { _type = value; }
            get { return _type; }
        }
    }

    /// <summary>
    /// QQMSN处理类
    /// </summary>
    public class BQQMSN
    {
        public string path = HttpContext.Current.Server.MapPath("/App_Data/QQMSN.xml");
        /// <summary>
        /// 获取集合
        /// </summary>
        /// <param name="strWhere"></param>
        /// <param name="sort"></param>
        /// <returns></returns>
        public List<MQQMSN> getList(string strWhere)
        {
            DataSet ds = new DataSet();
            ds.ReadXml(path);
            if (ds.Tables.Count < 1)
            {
                return null;
            }

            DataRow[] rows = ds.Tables[0].Select(strWhere);

            if (rows == null || rows.Length < 1)
            {
                return null;
            }

            List<MQQMSN> list = DataRowToList(rows);
            list.Sort(SortOrdinal);
            return list;
        }

        /// <summary>
        /// 排序
        /// </summary>
        /// <param name="a1"></param>
        /// <param name="a2"></param>
        /// <returns></returns>
        private int SortOrdinal(MQQMSN a1, MQQMSN a2)
        {
            return a1.Ordinal.CompareTo(a2.Ordinal);
        }

        /// <summary>
        /// 获取对象
        /// </summary>
        /// <param name="GID"></param>
        /// <returns></returns>
        public MQQMSN getModel(string GID)
        {
            List<MQQMSN> list = getList("GID='" + GID + "'");
            if (list == null || list.Count < 1)
            {
                return null;
            }
            return list[0];
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="model"></param>
        public void Add(MQQMSN model)
        {
            XmlDocument doc = XMLHelper.xmlDoc(path);

            XmlNode SystemKeywordNode = doc.CreateElement("QQMSN");
            XmlNode GIDNode = doc.CreateElement("GID");
            GIDNode.InnerText = model.GID;
            XmlNode NumberNode = doc.CreateElement("Number");
            NumberNode.InnerText = model.Number;
            XmlNode AltNode = doc.CreateElement("Alt");
            AltNode.InnerText = model.Alt;
            XmlNode TypeNode = doc.CreateElement("Type");
            TypeNode.InnerText = model.Type.ToString();
            XmlNode OrdinalNode = doc.CreateElement("Ordinal");
            OrdinalNode.InnerText = model.Ordinal.ToString();
            XmlNode EnabledNode = doc.CreateElement("Enabled");
            EnabledNode.InnerText = model.Enabled.ToString();

            SystemKeywordNode.AppendChild(GIDNode);
            SystemKeywordNode.AppendChild(NumberNode);
            SystemKeywordNode.AppendChild(AltNode);
            SystemKeywordNode.AppendChild(TypeNode);
            SystemKeywordNode.AppendChild(OrdinalNode);
            SystemKeywordNode.AppendChild(EnabledNode);

            doc["QQMSNS"].AppendChild(SystemKeywordNode);
            doc.Save(path);
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="model"></param>
        public void Update(MQQMSN model)
        {
            Delete(model.GID);
            Add(model);
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="model"></param>
        public void UpdateEnabled(bool Enabled, string GID)
        {
            XmlDocument doc = XMLHelper.xmlDoc(path);
            XmlNodeList nodelist = doc["QQMSNS"].ChildNodes;
            foreach (XmlNode node in nodelist)
            {
                if (node["GID"].InnerText.Trim() == GID)
                {
                    node["Enabled"].InnerText = Enabled.ToString();
                    break;
                }
            }

            doc.Save(path);
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="model"></param>
        public void Delete(string GID)
        {
            XmlDocument doc = XMLHelper.xmlDoc(path);
            XmlNode node = null;
            foreach (XmlNode nd in doc["QQMSNS"].ChildNodes)
            {
                if (nd.Name == "QQMSN" && nd.ChildNodes[0].InnerText == GID)
                {
                    node = nd;
                }
            }
            if (node == null)
            {
                return;
            }
            doc["QQMSNS"].RemoveChild(node);
            doc.Save(path);
        }

        /// <summary>
        /// 将DataSet转成List
        /// </summary>
        /// <param name="ds"></param>
        /// <returns></returns>
        private List<MQQMSN> DataSetToList(DataSet ds)
        {
            if (ds == null || ds.Tables.Count < 1 || ds.Tables[0].Rows.Count < 1)
            {
                return null;
            }

            List<MQQMSN> list = new List<MQQMSN>();
            foreach (DataRow row in ds.Tables[0].Rows)
            {
                MQQMSN model = new MQQMSN();
                model.GID = row["GID"].ToString().Trim();
                model.Number = row["Number"].ToString().Trim();
                model.Alt = row["Alt"].ToString().Trim();
                model.Type = Convert.ToInt32(row["Type"]);
                model.Ordinal = Convert.ToInt32(row["Ordinal"]);
                model.Enabled = Convert.ToBoolean(row["Enabled"]);

                list.Add(model);
            }

            return list;
        }

        /// <summary>
        /// 将DataSet转成List
        /// </summary>
        /// <param name="ds"></param>
        /// <returns></returns>
        private List<MQQMSN> DataRowToList(DataRow[] rows)
        {
            if (rows == null || rows.Length < 1)
            {
                return null;
            }

            List<MQQMSN> list = new List<MQQMSN>();
            foreach (DataRow row in rows)
            {
                MQQMSN model = new MQQMSN();
                model.GID = row["GID"].ToString().Trim();
                model.Number = row["Number"].ToString().Trim();
                model.Alt = row["Alt"].ToString().Trim();
                model.Type = Convert.ToInt32(row["Type"]);
                model.Ordinal = Convert.ToInt32(row["Ordinal"]);
                model.Enabled = Convert.ToBoolean(row["Enabled"]);
                list.Add(model);
            }

            return list;
        }
    }
}
